package framework.module.webservice.client.controller;

import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;
import framework.common.ResponseVo;
import framework.module.webservice.client.vo.phoneNumber.MobileCodeWS;
import framework.module.webservice.client.vo.phoneNumber.MobileCodeWSSoap;
import framework.module.webservice.client.vo.weather.ArrayOfString;
import framework.module.webservice.client.vo.weather.WeatherWebService;
import framework.module.webservice.client.vo.weather.WeatherWebServiceSoap;
import framework.module.webservice.client.vo.zip.*;
import frameworkExt.util.controller.BaseControllerExt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.Document;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

/**
 * @author 吴宇亮 on 2019/8/9 0009 上午 9:12
 */
@Controller
@RequestMapping("/framework/module/webservice.do")
public class WebserviceController extends BaseControllerExt {

    @RequestMapping(params = "method=getMobileCodeInfo", produces="text/html; charset=UTF-8")
    @ResponseBody
    public String getMobileCodeInfo(HttpServletRequest request){
        MobileCodeWS service = new MobileCodeWS();
        MobileCodeWSSoap soap = service.getMobileCodeWSSoap();
        String mobileCodeInfo = soap.getMobileCodeInfo(request.getParameter("mobileCode"), "");
        return mobileCodeInfo;
    }

    @RequestMapping(params = "method=getWeatherbyCityName")
    @ResponseBody
    @Deprecated
    public ResponseVo getWeatherbyCityName(HttpServletRequest request){
        WeatherWebService service = new WeatherWebService();
        WeatherWebServiceSoap soap = service.getWeatherWebServiceSoap();
        ArrayOfString weatherbyCityName = soap.getWeatherbyCityName("59279");
        return sendSuccess(weatherbyCityName);
    }

    @RequestMapping(params = "method=getAddressByZipCode", produces = "application/xml;charset=UTF-8")
    @ResponseBody
    public String getAddressByZipCode(HttpServletRequest request) throws TransformerException {
        ChinaZipSearchWebService service = new ChinaZipSearchWebService();
        ChinaZipSearchWebServiceSoap soap = service.getChinaZipSearchWebServiceSoap();
        GetAddressByZipCodeResponse.GetAddressByZipCodeResult addressByZipCode = soap.getAddressByZipCode(request.getParameter("zipCode"), "");

        ElementNSImpl obj = (ElementNSImpl) addressByZipCode.getAny().get(1);
        Document document = obj.getOwnerDocument();
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString();
    }



}
